package cate.game.rank.cross;

import cate.common.table.d.GDRank;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.framework.ModuleMan;
import cate.game.rank.cross.h.BaseCrossRankLoader;
import cate.game.rank.cross.h.GuildCrossRankLoader;
import cate.game.rank.cross.h.HolyPetCrossRankLoader;
import cate.game.rank.cross.h.PowerCrossRankLoader;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import cate.game.role.res.guild.po.basic.GuildBasicPO;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 跨服排行管理
 */
public class CrossRankManager extends ModuleMan {
    public static final String MODULE_NAME = "cross.rank";

    /**
     * 跨服消息模块
     */
    @Getter
    private final CrossRankMessageModule messageModule = new CrossRankMessageModule();
    @Getter
    private final CrossRankRecvModule recvModule = new CrossRankRecvModule();

    /**
     * 跨服排行处理器
     */
    private final Map<Integer, BaseCrossRankLoader> crossRankLoaders = new HashMap<>();

    private GuildCrossRankLoader guildRankLoader;

    @Override
    public void initDependent() throws Exception {
        registerRankHandler(new PowerCrossRankLoader(messageModule, GDRank.Id.CROSS_POWER_RANK_ID));
        registerRankHandler(new HolyPetCrossRankLoader(messageModule, GDRank.Id.HOLY_PET_SUPPORT));
        guildRankLoader = new GuildCrossRankLoader(messageModule, GDRank.Id.CROSS_GUILD_RANK_ID);

        recvModule.initIndependent(game);
    }

    /**
     * 是否存在排行处理器
     *
     * @param rankId 排行ID
     */
    public boolean existCrossRankHandler(int rankId) {
        return crossRankLoaders.containsKey(rankId);
    }

    /**
     * 注册排行处理器
     *
     * @param loader 排行处理器
     */
    public void registerRankHandler(BaseCrossRankLoader loader) {
        crossRankLoaders.put(loader.getRankId(), loader);
    }

    /**
     * 获取排行处理器
     *
     * @param rankId 排行ID
     */
    public BaseCrossRankLoader getHandler(int rankId) {
        return crossRankLoaders.get(rankId);
    }

    /**
     * 更新排行数据
     *
     * @param role 角色
     * @param rankId 排行ID
     * @param value 排行值
     */
    public void recordValue(Role role, int rankId, long value) {
        recordValue(role, rankId, value, false);
    }

    /**
     * 更新排行数据
     *
     * @param role 角色
     * @param rankId 排行ID
     * @param value 排行值
     */
    public void recordValue(Role role, int rankId, long value, boolean force) {
        if (!crossRankLoaders.containsKey(rankId) || rankId == GDRank.Id.CROSS_GUILD_RANK_ID) {
            return;
        }
        crossRankLoaders.get(rankId).record(role, value, force);
    }

    public void recordGuildValue(GameBody game, RoleSnapshot rs, GuildBasicPO guildBasic) {
        guildRankLoader.updateGuild(game, rs, guildBasic);
    }

    /**
     * 跨服获取单榜数据
     *
     * @param role 角色
     * @param rankId 排行ID
     * @param limit 限制数量
     * @return 查询结果
     */
    public GameResult<Void> sendCrossSingleRankData(Role role, int rankId, int limit) {
        GameResult<Void> r = new GameResult<>();
        if (rankId == GDRank.Id.CROSS_GUILD_RANK_ID) {
            return guildRankLoader.sendCrossRankList(role, limit);
        }
        if (!crossRankLoaders.containsKey(rankId)) {
            return r.fail("不存在的榜单");
        }
        return crossRankLoaders.get(rankId).sendCrossRankList(role, limit);
    }

    public void offline(Role role) {
        recordValue(role, GDRank.Id.CROSS_POWER_RANK_ID, role.getBase().power, true);
    }
}
